/**
* <p>Title: TestDataHandler.java</p>
* <p>Description: 数据驱动</p>
* <p>Company: www.huoyunren.com</p>
* @author duanxiangyun
* @date 2015年1月7日
* @version 1.0
*/
package com.hxbd.test.handles;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.testng.Assert;

import com.duan.test.utils.Excel.DefaultExcelParser;
import com.duan.test.utils.Excel.IExcelParser;

/**
 * excel数据获取处理类：POI方式
 */
public class ExcelDataHandler {
	// prevent to create instance
	private static IExcelParser excelParser;
	private ExcelDataHandler() {
	}

	/**
	 * 初始化对象，加载excel
	 * @param filename 文件名
	 * @return excel对象
	 */
	public static ExcelDataHandler getInstance(String filename) {
		ExcelDataHandler handler = new ExcelDataHandler();
		excelParser = DefaultExcelParser.createExcelParser(filename);
		return handler;	
	}

	/**
	 * 获得sheet对应数据
	 * @param sheetname 表名
	 * @return 二维数据的数据对象
	 */
	public Object[][] getData(String sheetname) {			
			Workbook wb;
			Sheet sheet;
			Object[][] tempData = null;
			try {
				wb = excelParser.getWorkBook();
				if (null == wb) {
					tempData = null;
				}
				// get the first sheet
				sheet = excelParser.getSheet(wb, sheetname);
				if (null == sheet) {
					Assert.fail("Excel找不到对应sheet：" + sheetname);
				}
				tempData = convertSheet2Array(sheet);			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Assert.fail("Excel读取失败,请确认excel文件路径是否正确");
			}
			return tempData;
	}

	/**
	 * Description: convert sheet data.
	 * @param sheet 表名
	 * @return Object[][] 对象数组 ;没有数据返回null
	 */
	private static Object[][] convertSheet2Array(Sheet sheet) {
		int rows = sheet.getLastRowNum() + 1;
		int cols = sheet.getRow(0).getLastCellNum();
		// locate the last valid row
		for (; rows >= 0; rows--) {
			if (excelParser.getCellValue(sheet, rows - 1, 0).trim().length() != 0) {
				break;
			}
		}

		if (rows < 1) {
			return null;
		}

		// locate the last valid column
		for (; cols >= 0; cols--) {
			if (excelParser.getCellValue(sheet, 0, cols - 1).trim().length() != 0) {
				break;
			}
		}
		if (cols < 1) {
			return null;
		}

		// get rid of the first line
		rows--;
		cols--;
		Object[][] datas = new Object[rows][cols];

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if(excelParser.getCellValue(sheet, i + 1, 0).equalsIgnoreCase("N"))
				{
				}
				else {
					String value = excelParser.getCellValue(sheet, i + 1, j + 1);
					if (value == null) {
						value = "";
					}
					if (value.equalsIgnoreCase("Y")) {
						datas[i][j] = Boolean.TRUE;
					} else if (value.equalsIgnoreCase("N")) {
						datas[i][j] = Boolean.FALSE;
					} else{
						datas[i][j] = value;
					}
				}					
			}
		}
		List<Object[]> list = new ArrayList<Object[]>();
		for (Object[] objs : datas) {
			Boolean isDel = true;
			for2:
			for (Object o : objs) {
				if (o != null) {
					isDel = isDel && false;
					break for2;
				}
			}
			if (!isDel) {
				list.add(objs);
			}
		}
		Object[][] datass = new Object[list.size()][cols];
		for (int i=0;i<list.size();i++) {
			datass[i] = list.get(i);
		}
		return datass;
	}
}